package com.nsgf.library.ui.page;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.databinding.ActivityNsgfIntroduceWebviewBinding;
import com.nsgf.library.databinding.ActivityNsgfWebviewBinding;

public class WebViewIntroduceActivity extends BaseActivity {


    private StateHolder mState;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
    }

    private ActivityNsgfIntroduceWebviewBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_introduce_webview, BR.vm, mState);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = (ActivityNsgfIntroduceWebviewBinding) getBinding();
        mBinding.webView.loadUrl(getIntent().getStringExtra("url"));
        mBinding.titlebar.getRoot().setBackgroundColor(getResources().getColor(R.color.transparent));

        mState.titleBar.set(getIntent().getStringExtra("title"));

        mBinding.titlebar.titleBarRoot.setTextColor(getResources().getColor(R.color.white));
        mBinding.titlebar.titleBarBack.setImageResource(R.drawable.jpush_ic_richpush_actionbar_back);
//
//
//        //获取当前html的title 并赋值给webview_title
//        mBinding.webView.getWebView().setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                // TODO Auto-generated method stub
//                super.onReceivedTitle(view, title);
//                mBinding.titlebar.titleBarRoot.setText(title);
//                //titles.add(title);
//            }
//        });
        mBinding.webView.getWebView().addJavascriptInterface(new TestInterface(), "android");

    }

    /**
     * Js调用的JavascriptInterface
     */
    public class TestInterface {

        @JavascriptInterface
        public void callNativeMethod(String src) {
//            {"path":"/monitoring/osmoticPressure","title":"渗压监测"}
            Gson gson = new Gson();
            LibraryInfo info = gson.fromJson(src, LibraryInfo.class);
            // 当用户点击链接时，不在WebView中打开，而是启动新的Activity
            Intent intent = new Intent(WebViewIntroduceActivity.this, WebViewIntroduceActivity.class);
            intent.putExtra("url", APIs.HTTP_H5 + info.getPath());
            intent.putExtra("title", info.getTitle());
            startActivity(intent);
        }

    }

    /**
     * 返回键返回上一网页
     */
    @Override
    public void onBackPressed() {
        if (mBinding.webView.canGoBack()) {
            mBinding.webView.goBack();
        } else {
            finish();
        }

    }


}
